package servlet;

import dao.EmpDao;
import entity.Dept;
import entity.Emp;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionFactoryUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/empupdate")
public class EmpUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("text/html;charset=utf-8");
       String ename = request.getParameter("ename");
       String job = request.getParameter("job");
       int empno = Integer.parseInt(request.getParameter("empno"));
       double sal = Double.parseDouble(request.getParameter("sal"));
       String dname = request.getParameter("dname");
       Dept dept = new Dept();
       dept.setDname(dname);
       Emp emp = new Emp(empno,ename,job,dept,sal);
       SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
       EmpDao empDao = sqlSession.getMapper(EmpDao.class);
       int num=empDao.updateEmpById(emp);
       if(num>0){
           response.sendRedirect("emp.html");
       }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
